package service

import (
	"blog/config"
	"blog/dao"
	"blog/models"
)

func GetAllIndexInfo(slug string, page, pageSize int) (*models.HomeResponse, error) {
	//上面涉及的数据必须要定义
	//页面上涉及到的所有的数据，必须有定义
	categorys, err := dao.GetAllCategory()
	if err != nil {
		return nil, err
	}
	var posts []models.PostMore
	var total int
	if slug == "" {
		posts, _ = dao.GetPostMorePage(page, pageSize)
		total, _ = dao.CountGetAllPost()
	} else {
		posts, _ = dao.GetPostMorePageBySlug(slug, page, pageSize)
		total, _ = dao.CountGetAllPostBySlug(slug)
	}

	pagesCount := (total + pageSize - 1) / pageSize
	var pages []int
	for i := 0; i < pagesCount; i++ {
		pages = append(pages, i+1)
	}
	var hr = &models.HomeResponse{
		config.Cfg.Viewer,
		categorys,
		posts,
		total,
		page,
		pages,
		pagesCount != page,
	}
	return hr, nil
}
